package cn.xiaomubiao.oschine.fragment.myinfofragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import cn.xiaomubiao.oschine.MyApplication;
import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.adapter.myinfo.MyInfoBaseListAdapter;
import cn.xiaomubiao.oschine.util.ItemDivider;
import cn.xiaomubiao.oschine.util.UIUtils;

/**
 * @author
 * @version 1.0
 * @date 2017/4/5
 */

public abstract class MyInfoBaseListFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_myinfo_baselist_fragment, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.base_list_srl);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.base_list_rcv);
        initSwipeRefreshLayout();
        initRecyclerView();
        loadData();
    }

    private void initRecyclerView() {
        LinearLayoutManager manager = new LinearLayoutManager(UIUtils.getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        MyInfoBaseListAdapter adapter = getListAdapter();
        
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.addItemDecoration(new ItemDivider().setDividerWith(1).setDividerColor(Color.parseColor("#e7e7e7")));
        
    }


    private void initSwipeRefreshLayout() {
        mSwipeRefreshLayout.setColorSchemeColors(Color.parseColor("#ff0000"), Color.parseColor("#00ff00"), Color.parseColor("#0000ff"), Color.parseColor("#ffff00"));
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Toast.makeText(getActivity(), "refresh", Toast.LENGTH_SHORT).show();
                MyApplication.mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                },1500);
                
            }
        });
    }


    protected abstract MyInfoBaseListAdapter getListAdapter();
    protected abstract void loadData();

}
